export default function PaletteProvider(palette, create, elementFactory, handTool, lassoTool, spaceTool, globalConnect, translate) {
	this.create = create;
	this.elementFactory = elementFactory;
	this.handTool = handTool;
	this.lassoTool = lassoTool;
	this.spaceTool = spaceTool;
	this.globalConnect = globalConnect;
	this.translate = translate;

	palette.registerProvider(this);
}

PaletteProvider.$inject = ["palette", "create", "elementFactory", "handTool", "lassoTool", "spaceTool", "globalConnect", "translate"];

PaletteProvider.prototype.getPaletteEntries = function (element) {
	const { create, elementFactory, handTool, lassoTool, spaceTool, globalConnect, translate } = this;

	function createAction(type, group, className, title, options) {
		function createListener(event) {
			let shape = elementFactory.createShape(Object.assign({ type: type }, options));

			if (options) {
				shape.businessObject.di.isExpanded = options.isExpanded;
			}

			create.start(event, shape);
		}

		let shortType = type.replace(/^bpmn:/, "");

		return {
			group: group,
			className: className,
			title: title || translate("Create {type}", { type: shortType }),
			action: {
				dragstart: createListener,
				click: createListener,
			},
		};
	}
	function createSubprocess(event) {
		let subProcess = elementFactory.createShape({
			type: "bpmn:SubProcess",
			x: 0,
			y: 0,
			isExpanded: true,
		});

		let startEvent = elementFactory.createShape({
			type: "bpmn:StartEvent",
			x: 40,
			y: 82,
			parent: subProcess,
		});

		create.start(event, [subProcess, startEvent], {
			hints: {
				autoSelect: [startEvent],
			},
		});
	}
	function createParticipant(event) {
		create.start(event, elementFactory.createParticipantShape());
	}

	/**
	 * @param {String} group 定义这个shape属于哪个组，主要有tools、event、gateway和activity可以选择
	 * @param {String} className 定义这个shape的chass名称，可以通过这个class给shape指定具体的样式
	 * @param {String} title 定义这个shape的title，也就是鼠标移动到shape上的提示
	 * @param {Object} action 用户操作时触发的事件
	 */

	return {
		"hand-tool": {
			group: "tools",
			className: "bpmn-icon-hand-tool",
			title: translate("Activate the hand tool"),
			action: {
				click: function (event) {
					handTool.activateHand(event);
				},
			},
		},
		"lasso-tool": {
			group: "tools",
			className: "bpmn-icon-lasso-tool",
			title: translate("Activate the lasso tool"),
			action: {
				click: function (event) {
					lassoTool.activateSelection(event);
				},
			},
		},
		"space-tool": {
			group: "tools",
			className: "bpmn-icon-space-tool",
			title: translate("Activate the create/remove space tool"),
			action: {
				click: function (event) {
					spaceTool.activateSelection(event);
				},
			},
		},
		"global-connect-tool": {
			group: "tools",
			// className: 'bpmn-icon-connection-multi',
			className: "icon-custom icon-custom-flow",
			title: translate("Activate the global connect tool"),
			action: {
				click: function (event) {
					globalConnect.toggle(event);
				},
			},
		},
		"tool-separator": {
			group: "tools",
			separator: true,
		},
		"create.start-event": createAction(
			"bpmn:StartEvent",
			"event",
			/* 'bpmn-icon-start-event-none' */ "icon-custom icon-custom-start",
			translate("Create StartEvent")
		),
		"create.intermediate-event": createAction(
			"bpmn:IntermediateThrowEvent",
			"event",
			"bpmn-icon-intermediate-event-none",
			translate("Create Intermediate/Boundary Event")
		),
		"create.end-event": createAction(
			"bpmn:EndEvent",
			"event",
			/* 'bpmn-icon-end-event-none' */ "icon-custom icon-custom-end",
			translate("Create EndEvent")
		),
		"create.exclusive-gateway": createAction(
			"bpmn:ExclusiveGateway",
			"gateway",
			/* 'bpmn-icon-gateway-none' */ "icon-custom icon-custom-exclusive-gateway",
			translate("Create Gateway")
		),
		"create.task": createAction("bpmn:Task", "activity", /* 'bpmn-icon-task' */ "icon-custom icon-custom-task", translate("Create Task")),
		"create.dataObjectReference": createAction("bpmn:DataObjectReference", "activity", "icon-custom icon-custom-data", translate("变量")),
		"create.businessRule-task": createAction(
			"bpmn:BusinessRuleTask",
			"activity",
			"icon-custom icon-custom-businessRule",
			translate("businessRule任务")
		),
		"create.data-object": createAction("bpmn:DataObjectReference", "data-object", "bpmn-icon-data-object", translate("Create DataObjectReference")),
		"create.data-store": createAction("bpmn:DataStoreReference", "data-store", "bpmn-icon-data-store", translate("Create DataStoreReference")),
		"create.subprocess-expanded": {
			group: "activity",
			className: "bpmn-icon-subprocess-expanded",
			title: translate("Create expanded SubProcess"),
			action: {
				dragstart: createSubprocess,
				click: createSubprocess,
			},
		},
		"create.participant-expanded": {
			group: "collaboration",
			className: "bpmn-icon-participant",
			title: translate("Create Pool/Participant"),
			action: {
				dragstart: createParticipant,
				click: createParticipant,
			},
		},
		"create.group": createAction("bpmn:Group", "artifact", "bpmn-icon-group", translate("Create Group")),
	};
};
